//
//  TitleView.m
//  TodatPlay
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "TitleView.h"
#import "TAMacro.h"
#import "NetEaseTitleLayout.h"
#import "TitleCollectionViewCell.h"
#import "ClassifyCategorydoc.h"
#import "UIView+Frame.h"
#define kCollectionTitleViewReuse @"titleReuse"
@interface TitleView ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, NetEaseTitltLayoutDelegate>

@property (nonatomic, strong) NetEaseTitleLayout *layout;
@property (nonatomic, copy)didClickIndexPath index;

@end

@implementation TitleView {
    NSInteger number;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
#pragma mark - 实现block方法
- (void)didClickIndexPath:(didClickIndexPath)index {
    self.index = index;
}


- (void)createCollectionView {
    self.layout = [[NetEaseTitleLayout alloc] init];
    _layout.titleFont = [UIFont systemFontOfSize:17];
    _layout.delegate = self;
    self.titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) collectionViewLayout:_layout];
    _titleCollectionView.backgroundColor = [UIColor whiteColor];
    _titleCollectionView.showsHorizontalScrollIndicator = YES;
    _titleCollectionView.delegate = self;
    _titleCollectionView.dataSource = self;
    [self addSubview:_titleCollectionView];
    [self.titleCollectionView registerNib:[UINib nibWithNibName:@"TitleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionTitleViewReuse];
    self.titleCollectionView.bounces = NO;
    self.titleCollectionView.showsHorizontalScrollIndicator = NO;
    [self createLineView];

}

- (void)createLineView {
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 37, 20, 3)];
    [self.titleCollectionView addSubview:_lineView];
    _lineView.backgroundColor = [UIColor blackColor];
    
}
- (void)setCategoryArr:(NSArray<ClassifyCategorydoc *> *)categoryArr {
    _categoryArr = categoryArr;

    [self.titleCollectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _categoryArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionTitleViewReuse forIndexPath:indexPath];
    cell.titleLabel.text = _categoryArr[indexPath.row].name;
    cell.titleLabel.textAlignment = NSTextAlignmentCenter;
    cell.titleLabel.font = _layout.titleFont;
    number = indexPath.row;
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout string:(NSString *)string stringFont:(UIFont *)font widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = _categoryArr[indexPath.row].name;
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layout.titleFont}
                                       context:nil];
    

    return newRect.size.width + 0.5;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    NSIndexPath *index = [NSIndexPath indexPathForRow:number inSection:0];
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[self.titleCollectionView cellForItemAtIndexPath:index];
    
    self.lineView.dm_width = cell.contentView.dm_width;
}
#warning user convert come ture thish CGRect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 调用block
    self.index(indexPath);
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[self.titleCollectionView cellForItemAtIndexPath:indexPath];
    self.lineView.dm_width = cell.contentView.dm_width;
//    NSLog(@"点击的%@", NSStringFromCGRect(cell.contentView.frame));
    CGRect newRect = [self convertRect:cell.contentView.frame fromView:cell];
     NSLog(@"点击view点击的%@", NSStringFromCGRect(newRect));
    self.lineView.dm_x = newRect.origin.x;
    CGRect lineNewRect = [self convertRect:self.lineView.frame fromView:self.titleCollectionView];
    lineNewRect.origin.x = newRect.origin.x;
    self.lineView.frame = lineNewRect;
//    self.lineView.dm_x = newRect.origin.x;
    NSLog(@"%@", NSStringFromCGRect(self.lineView.frame));
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    /***************** 判断是不是向右滑动 ******************/
    NSIndexPath *index = [NSIndexPath indexPathForRow:number inSection:0];
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)
    [self.titleCollectionView cellForItemAtIndexPath:index];
    NSLog(@"%@", _nameStr);
    
//    NSLog(@"华东的%@", NSStringFromCGRect(cell.contentView.frame));
    CGRect newRect = [self convertRect:cell.frame fromView:self.titleCollectionView];
//    NSLog(@"滑动view点击的%@", NSStringFromCGRect(newRect));
     static CGFloat lastOffsetX = 0;
    _isScrollRight = lastOffsetX < scrollView.contentOffset.x;
    lastOffsetX = scrollView.contentOffset.x;
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
